package com.woniuxy.mapper;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author 作者
 * @create 2025-09-25 14:34
 */
public class MybatisUtil {

    public static SqlSessionFactory sqlSessionFactory ;
    //饿汉
    static{
        try {
            String resource = "mybatis-config.xml";
            //加载主配置文件
            InputStream inputStream = Resources.getResourceAsStream(resource);
            //创建SqlSessionFactory工厂
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static SqlSession createSqlSession() {
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        return sqlSession;
    }
}
